#ifndef VIDEOPLAYER_H
#define VIDEOPLAYER_H

#include <QObject>
#include <QFile>
#include <QDebug>

extern "C" {

    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
    #include <libavutil/avutil.h>
}

class VideoPlayer : public QObject
{
    Q_OBJECT
public:

    enum VideoPlayerState{

        Stop,
        Playing,
        Pause
    };

    explicit VideoPlayer(QObject *parent = nullptr);
    ~VideoPlayer();

    void setVideoPath(const char* videoPath);

    void play();
    void stop();
    void pause();

    VideoPlayerState getState();

    int64_t getDuration();

signals:
    void stateChange(VideoPlayer* videoPlayer);
    void playError(VideoPlayer* videoPlayer);
    void initFinish(VideoPlayer* videoPlayer);

private:
    const char* _videoPath = nullptr;
    VideoPlayerState _state = Stop;
//    QFile* _videoFile = nullptr;

    AVFormatContext* _fmtContext = nullptr;

    AVCodecContext* _videoCodecContext = nullptr;
    AVCodecContext* _audioCodecContext = nullptr;
    AVStream* _videoStream = nullptr;
    AVStream* _audioStream = nullptr;

    void readVideoFile();

    int init(AVMediaType type,AVCodecContext** codecContext, AVStream** stream);

    int initVideo();
    int initAudio();

    void setState(VideoPlayerState state);
};

#endif // VIDEOPLAYER_H
